<template>
  <div>
    <el-form :model="dataForm" @keyup.enter.native="getDataList()" label-width="80px">
      <el-row :gutter="10">
        <el-col :xs="24" :sm="24" :md="12" :lg="6" :xl="6">
          <el-form-item label="企业名称" prop="title">
            <el-input v-model="dataForm.title" type="text" placeholder="客户名称" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="24" :md="12" :lg="6" :xl="6">
          <el-form-item label="法人" prop="legal_person">
            <el-input v-model="dataForm.legal_person" type="text" placeholder="法人" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="24" :md="12" :lg="6" :xl="6">
          <el-form-item label="录入时间" prop="sale">
            <el-date-picker
              v-model="dataForm.created_at"
              type="daterange"
              align="right"
              unlink-panels
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              value-format="yyyy-MM-dd"
              :picker-options="pickerOptions">
            </el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="10">
        <el-col :xs="20" :sm="20" :md="12" :lg="12" :xl="12">
          <el-form-item>
            <el-button type="" @click="getDataList()">查询</el-button>
            <el-button v-if="isAuth('company:create')" type="primary" @click="jumpTourl('company-create')">新增</el-button>
            <el-button v-if="isAuth('company:delete')" type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-table
      :data="dataList"
      border
      :row-class-name="rowClassName"
      v-loading="dataListLoading"
      @sort-change="changeSort"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        prop="company_id"
        header-align="center"
        align="center"
        width="80"
        label="ID">
      </el-table-column>
      <el-table-column
        prop="username"
        header-align="center"
        align="center"
        label="登录账号">
      </el-table-column>
      <el-table-column
        prop="title"
        header-align="center"
        align="center"
        label="企业名称">
      </el-table-column>
      <el-table-column
        prop="tax_number"
        header-align="center"
        align="center"
        width="240"
        label="纳税人识别号">
      </el-table-column>
      <el-table-column
        prop="legal_person"
        header-align="center"
        align="center"
        width="90"
        label="法人">
      </el-table-column>
      <el-table-column
        prop="legal_person_card"
        header-align="center"
        align="center"
        width="200"
        label="法人身份证号">
      </el-table-column>
      <el-table-column
        prop="rate"
        header-align="center"
        align="center"
        width="120"
        label="综合服务费率">
      </el-table-column>
      <el-table-column
        prop="status"
        header-align="center"
        align="center"
        width="70"
        label="状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.state === '禁用'" size="small" type="info">禁用</el-tag>
          <el-tag v-else size="small">正常</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="created_at"
        header-align="center"
        align="center"
        width="180"
        sortable="custom"
        sort-change="changeSort"
        label="录入时间">
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="140"
        label="操作">
        <template slot-scope="scope">
          <el-button v-if="isAuth('company:update')" type="text" size="small" @click="jumpTourl('company-update', scope.row.company_id)">修改</el-button>
          <el-button v-if="isAuth('company:delete')" type="text" size="small" @click="deleteHandle(scope.row.company_id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[15, 30, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
  </div>
</template>

<script>
  export default {
    data () {
      return {
        dataForm: {
          title: '',
          legal_person: '',
          created_at: '',
          sort_by: '',
          order_by: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 15,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        pickerOptions: {
          shortcuts: [{
            text: '最近一周',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit('pick', [start, end]);
            }
          }]
        }
      }
    },
    components: {
    },
    activated () {
      this.getDataList()
    },
    methods: {
      rowClassName({row, rowIndex}) {
        if(row.is_jg) {
          return 'bgred';
        }
        return ''
      },
      changeSort(val) {
        if (val.order !== null) {
          this.dataForm.sort_by = val.prop
          this.dataForm.order_by = val.order === 'ascending' ? 'asc' : 'desc'
        } else {
          this.dataForm.sort_by = ''
          this.dataForm.order_by = ''
        }
        this.dataListLoading = true
        this.getCustomerList()
        this.dataListLoading = false
      },
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.getCustomerList()
        this.dataListLoading = false
      },
      getCustomerList() {
        this.$http({
          url: this.$http.adornUrl('/company/list'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'title': this.dataForm.title,
            'legal_person': this.dataForm.legal_person,
            'created_at': this.dataForm.created_at,
            'sort': this.dataForm.sort_by,
            'order': this.dataForm.order_by
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
            this.$message.error(data.msg)
          }
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      jumpTourl ( _url, _id) {
        this.$router.push({ name: _url, query: { id:  _id}})
      },
      // 删除
      deleteHandle (id) {
        var customerIds = id ? [id] : this.dataListSelections.map(item => {
          return item.customer_id
        })
        this.$confirm(`确定对[id=${customerIds.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/sys/customer/delete'),
            method: 'post',
            data: this.$http.adornData({'customer_ids' : customerIds}, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }).catch(() => {
          console.log('取消操作')
          //this.$message.error('请求数据错误')
        })
      }
    }
  }
</script>

<style lang="scss">
  .bgred {
    background-color: #fab6b6 !important;
  }
</style>
